package com.gdut.simple.view.activity.common;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.gdut.simple.R;
import com.gdut.simple.view.adapter.PhotoViewPagerAdapter;
import com.gdut.simple.view.widget.PhotoViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PhotoPreviewActivity extends Activity {

    @BindView(R.id.id_photo_view_pager)
    PhotoViewPager mPhotoViewPager;
    @BindView(R.id.id_photo_position)
    TextView mPhotoPosition;

    private PhotoViewPagerAdapter mPhotoAdapter;

    private List<String> mImagesList;
    private int mTotalCount;
    private int mPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_view);
        ButterKnife.bind(this);
        initViewPager();
    }

    private void initViewPager() {
        Intent intent = getIntent();
        if (intent != null) {
            mImagesList = intent.getStringArrayListExtra("images");
            mPosition = intent.getIntExtra("position", 0);
            if (mImagesList == null) {
                return;
            }
            mTotalCount = mImagesList.size();
            mPhotoPosition.setText((mPosition + 1) + "/" + mTotalCount);
            mPhotoAdapter = new PhotoViewPagerAdapter(this, mImagesList);
            mPhotoViewPager.setAdapter(mPhotoAdapter);
            mPhotoViewPager.setCurrentItem(mPosition);
            mPhotoViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

                @Override
                public void onPageSelected(int position) {
                    super.onPageSelected(position);
                    mPosition = position;
                    mPhotoPosition.setText((mPosition + 1) + "/" + mTotalCount);
                }
            });
        }
    }

    public static void startActivity(Activity context, ArrayList<String> images, int position) {
        Intent intent = new Intent(context, PhotoPreviewActivity.class);
        intent.putStringArrayListExtra("images", images);
        intent.putExtra("position", position);
        context.startActivity(intent);
        context.overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out);
    }
}
